package com.wjw.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wjw.entity.User;
import com.wjw.service.IUserService;



@Controller
public class AdminUserController {
	@Resource
	private IUserService userService;
	
	//查询所有的用户
	
	@RequestMapping(value="/listUser")
	public String listUser(Model model){
		List<User> users=userService.selectList(null);
		System.out.println("===========炜哥");
		model.addAttribute("users",users);
		return "/user/list";
	}
	//更新用户
	@RequestMapping(value="/updateUser")
	public ModelAndView updateUser(@ModelAttribute("user") User user){
		userService.updateById(user);
		ModelAndView model = new ModelAndView();
		model.setViewName("redirect:/listUser/1");
		return model;
	}
	//修改用户
	@RequestMapping(value="/editUser/{uid}")
	public String editUser(@PathVariable("uid") Integer uid,@ModelAttribute("user") User user,Map<String,Object> map){
		user = userService.findTByTOne(user);
		map.put("user", user);
		return "admin/user/edit";
	}
	
	//删除用户 uid:用户的id page:当前第几页
	@RequestMapping(value="/deleteUser/{uid}/{page}")
	public ModelAndView deleteUser(@PathVariable("uid") Integer uid,@PathVariable("page") Integer page){
		userService.deleteById(uid);
		ModelAndView model = new ModelAndView();
		model.setViewName("redirect:/listUser/"+page);
		return model;
	}
	
}
